import 'dart:convert';

import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/utils/kwargs.dart';
import 'package:flutter_admin/utils/odoo_http_util.dart';

class DmsFileOdooApi{
  static Future<ResponseBodyApi> page({data}) async {
    ResponseBodyApi responseBodyApi;
    try{
      PageModel pageModel = data['page'];
      List domainList = data['domainList'];
      List<String> fields = ['id','documentno', 'name','remark','create_uid','create_date'];
      Kwargs recordsKwargs = Kwargs(domain: domainList,limit: pageModel.size,offset: (pageModel.current-1)*pageModel.size,order: pageModel.orders[0].column!+" "+(pageModel.orders[0].asc!?"asc":"desc"),fields: fields);
      var records = await OdooHttpUtil.callKw(model:'dms.file',method:'search_read',kwargs:recordsKwargs.toMap());
      var total = await OdooHttpUtil.callKw(model:'dms.file',method:'search_count',kwargs: {"domain":domainList});
      List<Map<String,dynamic>> list=[];
      records.forEach((element) {
        Map<String, dynamic> map = element;
        list.add(map);
      });
      pageModel.records = list;
      pageModel.total = total;
      pageModel.pages = total~/pageModel.size+1;
      responseBodyApi = ResponseBodyApi(success: true, data: pageModel);
    }catch(e){
      responseBodyApi = ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> removeByIds(data) async {
    ResponseBodyApi responseBodyApi;
    try{
      await OdooHttpUtil.callKw(model: 'dms.file',method: "unlink", args: [data]);
      responseBodyApi = ResponseBodyApi(success: true);
    }catch(e){
      responseBodyApi = ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> upload(data) async {
    ResponseBodyApi responseBodyApi;
    try{
      await OdooHttpUtil.callKw(model: 'dms.file',method: 'create', args: [data]);
      responseBodyApi = ResponseBodyApi(success: true);
    }catch(e){
      responseBodyApi = ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }
  static Future<ResponseBodyApi> batchUpload(list) async {
    ResponseBodyApi responseBodyApi;
    try{
      await OdooHttpUtil.callKw(model: 'dms.file',method: 'create', args: [list]);
      responseBodyApi = ResponseBodyApi(success: true);
    }catch(e){
      responseBodyApi = ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> fileTransfer(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      await OdooHttpUtil.callKw(
          model: 'dms.file',
          method: 'write',
          args: [
            data["id"],
            data["data"]
          ]);
      responseBodyApi = ResponseBodyApi(success: true);
    } catch (e) {
      print(e.toString());
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> findByDocumentno(String? documentno) async{
    ResponseBodyApi responseBodyApi;
    try {
      List domainList = [
        ["documentno","=",documentno]
      ];
      List<String> fields = ['id','documentno', 'name','remark','create_uid','create_date'];
      Kwargs kwargs = Kwargs(domain: domainList,fields: fields);
      var data = await OdooHttpUtil.callKw(
          model: 'dms.file',
          method: 'search_read',
          kwargs: kwargs.toMap()
      );
      responseBodyApi = ResponseBodyApi(success: true,data:data);
    } catch (e) {
      print(e.toString());
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }



}